#ifndef NETWORK_ZINGME_NETWORKZINGMEWEBSOCKET_H
#define NETWORK_ZINGME_NETWORKZINGMEWEBSOCKET_H

#include <QList>
#include <QSemaphore>
#include <QThread>
#include <QVariant>
#include <QAbstractSocket>


QT_FORWARD_DECLARE_CLASS(QWebSocket)
QT_FORWARD_DECLARE_CLASS(QTimer)

namespace Network {

class ConnectionRequest;

namespace Zingme {

class WebSocket: public QObject
{
    Q_OBJECT
public:
    explicit WebSocket(QObject *parent = 0);

public slots:
    void connect(const QString &url, unsigned int port, const QVariant &params);
    void send(Network::ConnectionRequest *request);
    void disconnect();

public:
    bool isConnected() const;

private slots:
    void onConnected();
    void onReceived(QByteArray message);
    void onReceivedTextMessage(QString message);
    void onDisconnected();
    void sendPing();
    void onPong(quint64 elapsedTime, const QByteArray &payload);
public slots:
    void onSocketError(QAbstractSocket::SocketError);

signals:
    void connected();
    void received(QByteArray message);
    void disconnected();
    void wantReconnect();

private:
    QWebSocket* m_socket;
    QTimer*     m_pingTimer;
    QTimer*     m_timeoutTimer;
};

} // namespace zingme

} // namespace network

#endif // NETWORK_ZINGME_NETWORKZINGMEWEBSOCKET_H
